/*
 * Created with Sublime Text 2.
 * license: http://www.lovewebgames.com/jsmodule/index.html
 * User: 田想兵
 * Date: 2015-04-27
 * Time: 10:27:55
 * Contact: 55342775@qq.com
 */
(function(root, factory) {
	//amd
	if (typeof define === 'function' && define.amd) {
		define(['$'], factory);
	} else if (typeof exports === 'object') { //umd
		module.exports = factory();
	} else {
		root.ScrollLoad = factory(window.Zepto || window.jQuery || $);
	}
})(this, function($) {
	$.fn.CarouselImage = function(settings) {
		var list = [];
		$(this).each(function() {
			var car = new CarouselImage();
			var options = $.extend({
				target: $(this),
				index:1,
                sumnum:0,
				autoing:false,
				backnumall:function(){},
				backnum:function(){},
                gotoleft:function(){},
                gotoright:function(){}
			}, settings);
			car.init(options);
			list.push(car);
		});
		return list;
	};

	function CarouselImage() {}
	CarouselImage.prototype = {
		init: function(settings) {
			this.index=settings.index;
            this.gotoleft=settings.gotoleft;
            this.gotoright=settings.gotoright;
			this.backnumall=settings.backnumall;
			this.backnum=settings.backnum;
			this.sumnum=settings.sumnum;
			this.container = settings.target;
			this.autoing = settings.autoing;
			this.content = this.container.children().first();
			this.timer = settings.timer || 3000;
			this.animate = settings.animate || 500;
			this.num = settings.num || null;
			this.list = this.content.children();
			this.step = document.documentElement.clientWidth;
		    $('.carousel-image a').css('width',this.step+'px');
			this.content.width(this.list.length * this.step);
			this.size = this.list.length;
			this.content.css({
				left: 0,
				position: "absolute"
			});
			// alert(this.content.width())
			this.bindEvent();
			if(this.autoing){
			   this.auto();
			}
			
			this.formatNum();
		},
		touch: function(obj, trigger, fn) {
			var move;
			var istouch = false;
			if (typeof trigger === "function") {
				fn = trigger;
			};
			$(obj).on('touchmove', trigger, function(e) {
				move = true;
			}).on('touchend', trigger, function(e) {
				e.preventDefault();
				move =false;
				if (!move) {
					var returnvalue = fn.call(this, e, 'touch');
					if (returnvalue === false) {
						e.preventDefault();
						e.stopPropagation();
					}
				}
				move = false;
			});
			$(obj).on('click', trigger, click);
			function click(e) {
				return fn.call(this, e);
			}
		},
		bindEvent: function() {
			var _this = this;
			var start = {},
				istartleft = 0,
				end = {},
				move = false;
			var curPos = {};
			this.content.on('swipeLeft', function(e) {
                _this.index++;
                console.log(' _this.sumnum'+ _this.sumnum);
                //console.log('left'+_this.index);
                if (_this.index >= _this.sumnum) {
                    _this.index = _this.sumnum;

                }
                _this.gotoleft( _this.index);

				return false;
			});
			this.content.on('swipeRight', function(e) {
                console.log( _this.index);
                _this.index--;
                if (_this.index <= 1) {
                    _this.index =1;
                }
                    //console.log('right'+_this.index);
                    _this.gotoright( _this.index);



				return false;
			});
//			_this.touch(_this.num,"i",  function() {
//				clearInterval(_this.interval);
//				_this.index = $(this).index();
//				_this.go();
//				if(this.autoing){
//			   _this.auto();
//			    }
//			});
		},
		formatNum: function() {
			if (this.num) {
				var html = '';
				var width=0;
				var sum=0;
				for (var i = 0, l = this.list.length; i < l; i++) {
					sum++;
					width+=0.16;
					var item = this.list[i];
					var cls = '';
					if (this.index == i) {
						cls = 'current';
					}
					html += '<i class="' + cls + '">' + (i + 1) + '</i>';
				};
				this.backnumall(sum);
				this.num.html(html);
				$('.carousel-num').css('width',width+'rem');
			}
		},
		go: function() {
			
			var _this = this;
			
			if (_this.index >= _this.sumnum) {
				_this.index = _this.sumnum - 1;
			}
			if (_this.index < 0) {
				_this.index = 0
			}
			var step = _this.step;
			var left = -_this.index * step;
			console.log(_this.index);
			this.backnum(_this.index);
            //_this.content.animate({
            //    left: left
            //}, _this.animate, $.proxy(_this.formatNum, _this));
		},
		auto: function() {
			var _this = this;
			this.interval = setInterval(function() {
				_this.index++;
				if (_this.index >= _this.sumnum) {
					_this.index = 0;
				}
				_this.go();
			}, this.timer);
		}
	}
	return CarouselImage;
});